using System;

namespace SG3L_RAG.Domain.Entities
{
    public class Message : EntityBase
    {
        public Guid ConversationId { get; private set; }
        public Conversation? Conversation { get; private set; }
        public string Sender { get; private set; } = string.Empty;
        public string Content { get; private set; } = string.Empty;
        public string Reply { get; private set; } = string.Empty;
        public Guid? ReferencedDocId { get; private set; }
        public Document? ReferencedDocument { get; private set; }

        // EF Core 需要的无参构造函数
        public Message() { }

        // 业务构造函数
        private Message(Guid conversationId, string sender, string content, string reply, Guid? referencedDocId = null)
        {
            ConversationId = conversationId;
            Sender = sender;
            Content = content;
            Reply = reply;
            ReferencedDocId = referencedDocId;
            CreatedAt = DateTime.UtcNow;
        }

        // 工厂方法
        public static Message Create(Guid conversationId, string sender, string content, string reply, Guid? referencedDocId = null)
        {
            return new Message(conversationId, sender, content, reply, referencedDocId);
        }

        public bool IsFromUser() => Sender == "user";
        public bool IsFromAI() => Sender == "ai";

        // 可信度评分（假设有此字段）
        public float ConfidenceScore { get; private set; }
        public void SetConfidenceScore(float score)
        {
            ConfidenceScore = score;
        }

        // 判断是否引用了文档
        public bool HasReferencedDocument() => ReferencedDocId.HasValue && ReferencedDocument != null;

        // 更新回复内容
        public void UpdateReply(string reply)
        {
            Reply = reply ?? string.Empty;
        }
    }
}
